package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.domain.entity.SysDept;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.ServletUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctMaterialBo;
import com.rzdata.poct.system.domain.business.po.PoctMaterial;
import com.rzdata.poct.system.domain.business.po.PoctMaterialInstrument;
import com.rzdata.poct.system.domain.business.po.PoctMaterialProject;
import com.rzdata.poct.system.domain.business.vo.PoctMaterialVo;
import com.rzdata.poct.system.mapper.PoctMaterialInstrumentMapper;
import com.rzdata.poct.system.mapper.PoctMaterialMapper;
import com.rzdata.poct.system.mapper.PoctMaterialProjectMapper;
import com.rzdata.poct.system.service.IPoctMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctMaterialServiceImpl extends ServicePlusImpl<PoctMaterialMapper, PoctMaterial, PoctMaterialVo> implements IPoctMaterialService {

    @Autowired
    private  PoctMaterialMapper poctMaterialMapper;

    @Autowired
    private PoctMaterialProjectMapper poctMaterialProjectMapper;

    @Autowired
    private PoctMaterialInstrumentMapper poctMaterialInstrumentMapper;

    @Override
    public PoctMaterialVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctMaterialVo> queryPageList(PoctMaterialBo bo) {
        PagePlus<PoctMaterial, PoctMaterialVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctMaterialVo> queryList(PoctMaterialBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctMaterial> buildQueryWrapper(PoctMaterialBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctMaterial> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialId()), PoctMaterial::getMaterialId, bo.getMaterialId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), PoctMaterial::getMaterialName, bo.getMaterialName());
        lqw.eq(bo.getDeptId() != null, PoctMaterial::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), PoctMaterial::getModel, bo.getModel());
        lqw.eq(bo.getType() != null, PoctMaterial::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), PoctMaterial::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), PoctMaterial::getUnit, bo.getUnit());
        lqw.eq(bo.getStatus() != null, PoctMaterial::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getManufacturer()), PoctMaterial::getManufacturer, bo.getManufacturer());
        lqw.eq(bo.getManufacturingDate() != null, PoctMaterial::getManufacturingDate, bo.getManufacturingDate());
        lqw.eq(bo.getShelflife() != null, PoctMaterial::getShelflife, bo.getShelflife());
        lqw.eq(StringUtils.isNotBlank(bo.getPackAge()), PoctMaterial::getPackAge, bo.getPackAge());
        lqw.eq(bo.getInventory() != null, PoctMaterial::getInventory, bo.getInventory());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctMaterialBo bo) {
        PoctMaterial add = BeanUtil.toBean(bo, PoctMaterial.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctMaterialBo bo) {
        PoctMaterial update = BeanUtil.toBean(bo, PoctMaterial.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctMaterial entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public TableDataInfo<PoctMaterialVo> queryPageMaterialList(PoctMaterialBo bo) {
        PoctMaterial queryPageMaterial = BeanUtil.toBean(bo, PoctMaterial.class);
        return PageUtils.buildDataInfo(poctMaterialMapper.queryPageMaterialList(PageUtils.buildPage(), queryPageMaterial));
    }

    @Override
    @Transactional
    public void insertMaterialInfo(PoctMaterialBo bo) {
        PoctMaterial poctMaterialBeen = BeanUtil.toBean(bo, PoctMaterial.class);
        poctMaterialMapper.insert(poctMaterialBeen);

        PoctMaterialProject poctMaterialProjectBeen = BeanUtil.toBean(bo, PoctMaterialProject.class);
        poctMaterialProjectMapper.insert(poctMaterialProjectBeen);

        PoctMaterialInstrument poctMaterialInstrumentBeen = BeanUtil.toBean(bo, PoctMaterialInstrument.class);
        poctMaterialInstrumentMapper.insert(poctMaterialInstrumentBeen);
    }

    @Override
    public PoctMaterialVo queryDetailById(Long id) {
        PoctMaterial poctMaterial = poctMaterialMapper.queryDetailById(id);
        return   BeanUtil.toBean(poctMaterial, PoctMaterialVo.class);
    }

    @Override
    public void updateMaterialStatus(PoctMaterial bo) {
        poctMaterialMapper.updateMaterialStatus(bo);
    }

    @Override
    @Transactional
    public void updateMaterialInfo(PoctMaterialBo bo) {

        PoctMaterial poctMaterialBeen = BeanUtil.toBean(bo, PoctMaterial.class);
        poctMaterialMapper.updateById(poctMaterialBeen);

        //这两个修改待确认
        PoctMaterialProject poctMaterialProjectBeen = BeanUtil.toBean(bo, PoctMaterialProject.class);
        poctMaterialProjectMapper.update(poctMaterialProjectBeen, new LambdaUpdateWrapper<PoctMaterialProject>()
            .set(PoctMaterialProject::getProjectId,  poctMaterialProjectBeen.getProjectId())
            .eq(PoctMaterialProject::getMaterialId, poctMaterialProjectBeen.getMaterialId()));
        //这两个修改待确认
        PoctMaterialInstrument poctMaterialInstrumentBeen = BeanUtil.toBean(bo, PoctMaterialInstrument.class);
        poctMaterialInstrumentMapper.update(poctMaterialInstrumentBeen, new LambdaUpdateWrapper<PoctMaterialInstrument>()
            .set(PoctMaterialInstrument::getInstrumentId,  poctMaterialInstrumentBeen.getInstrumentId())
            .eq(PoctMaterialInstrument::getMaterialId, poctMaterialInstrumentBeen.getMaterialId()));
    }
}
